<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>bayesdf.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>quad_model</span>=<span class=defun_name>bayesdf</span>(<span class=defun_in>model</span>)<br>
<span class=h1>%&nbsp;BAYESDF&nbsp;Computes&nbsp;decision&nbsp;boundary&nbsp;of&nbsp;Bayesian&nbsp;classifier.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;quad_model&nbsp;=&nbsp;bayesdf(model)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;computes&nbsp;parameters&nbsp;of&nbsp;decision&nbsp;boundary</span><br>
<span class=help>%&nbsp;&nbsp;of&nbsp;the&nbsp;Bayesian&nbsp;classifier&nbsp;with&nbsp;the&nbsp;following&nbsp;assumptions:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;-&nbsp;1/0&nbsp;loss&nbsp;function&nbsp;(risk&nbsp;=&nbsp;expectation&nbsp;of&nbsp;misclassification).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;-&nbsp;Binary&nbsp;classification.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;-&nbsp;Class&nbsp;conditional&nbsp;probabilities&nbsp;are&nbsp;multivariate&nbsp;Gaussians.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;In&nbsp;this&nbsp;case&nbsp;the&nbsp;Bayesian&nbsp;classifier&nbsp;has&nbsp;the&nbsp;quadratic&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;discriminant&nbsp;function</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f(x)&nbsp;=&nbsp;x'*A*x&nbsp;+&nbsp;B'*x&nbsp;+&nbsp;C,</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;where&nbsp;the&nbsp;classification&nbsp;strategy&nbsp;is</span><br>
<span class=help>%&nbsp;&nbsp;q(x)&nbsp;=&nbsp;1&nbsp;&nbsp;if&nbsp;f(x)&nbsp;&gt;=&nbsp;0,</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;2&nbsp;&nbsp;if&nbsp;f(x)&nbsp;&lt;&nbsp;0.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Two&nbsp;multi-variate&nbsp;Gaussians:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Mean&nbsp;[dim&nbsp;x&nbsp;2]&nbsp;Mean&nbsp;values.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Cov&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;2]&nbsp;Covariances.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Prior&nbsp;[1x2]&nbsp;A&nbsp;priory&nbsp;probabilities.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;quad_model.A&nbsp;[dim&nbsp;x&nbsp;dim]&nbsp;Quadratic&nbsp;term.</span><br>
<span class=help>%&nbsp;&nbsp;quad_model.B&nbsp;[dim&nbsp;x&nbsp;1]&nbsp;Linear&nbsp;term.</span><br>
<span class=help>%&nbsp;&nbsp;quad_model.C&nbsp;[1x1]&nbsp;Bias.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;trn&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;tst&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;gauss_model&nbsp;=&nbsp;mlcgmm(trn);</span><br>
<span class=help>%&nbsp;&nbsp;quad_model&nbsp;=&nbsp;bayesdf(gauss_model);</span><br>
<span class=help>%&nbsp;&nbsp;ypred&nbsp;=&nbsp;quadclass(tst.X,quad_model);</span><br>
<span class=help>%&nbsp;&nbsp;cerror(ypred,tst.y)</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;ppatterns(trn);&nbsp;pboundary(quad_model);&nbsp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;BAYESCLS,&nbsp;QUADCLASS</span><br>
<span class=help>%</span><br>
<hr>
<br>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;18-oct-2005,&nbsp;VF,&nbsp;dealing&nbsp;with&nbsp;Cov&nbsp;given&nbsp;as&nbsp;vector&nbsp;repared</span><br>
<span class=help1>%&nbsp;01-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;19-sep-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;24.&nbsp;6.00&nbsp;V.&nbsp;Hlavac,&nbsp;comments&nbsp;into&nbsp;English.</span><br>
<br>
<hr>
<span class=comment>%&nbsp;allow&nbsp;input&nbsp;to&nbsp;be&nbsp;a&nbsp;cell</span><br>
model&nbsp;=&nbsp;c2s(model);<br>
<br>
<span class=comment>%&nbsp;univariate&nbsp;variances&nbsp;can&nbsp;be&nbsp;given&nbsp;as&nbsp;a&nbsp;vector</span><br>
<span class=keyword>if</span>&nbsp;size(model.Cov,1)&nbsp;==&nbsp;1&nbsp;&&&nbsp;length(size(model.Cov))&nbsp;&lt;&nbsp;3,&nbsp;<br>
&nbsp;&nbsp;model.Cov&nbsp;=&nbsp;reshape(model.Cov,1,1,length(model.Cov));&nbsp;<br>
<span class=keyword>end</span><br>
<br>
M1=model.Mean(:,1);<br>
M2=model.Mean(:,2);<br>
C1=model.Cov(:,:,1);<br>
C2=model.Cov(:,:,2);<br>
P1=model.Prior(1);<br>
P2=model.Prior(2);<br>
<br>
quad_model.A=(1/2)*(inv(C2)-inv(C1));<br>
quad_model.B=(M1<span class=quotes>'*inv(C1)-M2'</span>*inv(C2))';<br>
<br>
<span class=comment>%&nbsp;Treatment&nbsp;of&nbsp;the&nbsp;case&nbsp;when&nbsp;apriori&nbsp;probabilities&nbsp;are&nbsp;zero.</span><br>
<span class=comment>%&nbsp;log(0)=-inf;</span><br>
<span class=keyword>if</span>&nbsp;P1==0,<br>
&nbsp;&nbsp;&nbsp;quad_model.C=-inf;<br>
<span class=keyword>elseif</span>&nbsp;P2==0,<br>
&nbsp;&nbsp;&nbsp;quad_model.C=inf;<br>
<span class=keyword>else</span><br>
&nbsp;&nbsp;&nbsp;quad_model.C=(1/2)*(M2<span class=quotes>'*inv(C2)*M2-M1'</span>*inv(C1)*M1)+...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log(sqrt(det(C2)))-log(sqrt(det(C1)))+log(P1)-log(P2);<br>
<span class=keyword>end</span><br>
<br>
quad_model.fun&nbsp;=&nbsp;<span class=quotes>'quadclass'</span>;<br>
<br>
<span class=jump>return</span>;<br>
</code>
